// pages/timeline/liferecord/add.js
import { delImage, uploadLifeRecordImage, createLifeRecord } from '../../../api/timeline'
import { isEmpty } from '../../../utils/validator'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    liferecord: {
      content: '',
      timeline: '',
      images: []
    },
    disabled: true,  // 能否保存
    imgList: [],
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad(options) {
    this.setData({
      [`liferecord.timeline`]: options['timeline']
    })
  },

  saveTap() {
    createLifeRecord(this.data.liferecord).then(res => {
      wx.showToast({
        title: '保存成功',
        icon: 'none'
      })
      wx.reLaunch({
        url: '../../index/index'
      })
    })
  },
  // 内容输入
  contentInput(e) {
    this.setData({
      [`liferecord.content`]: e.detail.value
    })
    this.is_can_save()
  },
  is_can_save() {
    // 是否能够保存
    var liferecord = this.data.liferecord
    if (!isEmpty(liferecord.content) || liferecord.images.length != 0) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  ChooseImage() {
    wx.chooseImage({
      // count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        uploadLifeRecordImage(res.tempFilePaths).then(imgs => {
          let images = imgs.map(picture => JSON.parse(picture.data).data) //  上传成功的
          let imgidList = images.map(image => image.id)
          this.setData({
            [`liferecord.images`]: this.data.liferecord.images.concat(imgidList),
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
          this.is_can_save()
          wx.showToast({ title: '上传成功', mask: true })
        }).catch(() => wx.hideLoading())
      }
    });
  },

  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '主人',
      content: '确定要删除这张照片吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          const index = e.currentTarget.dataset.index  // 当前图片的序号
          const imgid = this.data.liferecord.images[index] //当前图片的id
          delImage(imgid).then(res => {
            this.data.imgList.splice(index, 1)
            this.data.liferecord.images.splice(index, 1)
            this.setData({
              imgList: this.data.imgList,
              liferecord: this.data.liferecord
            })
            this.is_can_save()
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})